package org.xdq.demo.url.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xdq.demo.base.*;
import org.xdq.demo.url.model.UrlInfo;
import org.xdq.demo.url.service.UrlService;

import java.util.List;

@RestController
@Slf4j
public class UrlController {

    @Autowired
    private UrlService urlService;

    @GetMapping("/")
    public Result urlList(){

        try{
            List<UrlInfo> list = urlService.getUrlList();

            return Result.OK(list);

        }catch(BusinessException e){
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        }catch(SysException e){
            log.error("系统错误！", e);//输出错误日志
            return Result.err(Result.CODE_ERR_SYS, "系统升级中....");
        }
    }

    @PostMapping("/")
    public Result addUrl(@RequestBody UrlInfo urlInfo){

        try{
            urlService.addUrl(urlInfo);

            return Result.OK();

        }catch(BusinessException e){
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());
        }catch(SysException e){
            log.error("系统错误！", e);//输出错误日志
            return Result.err(Result.CODE_ERR_SYS, "系统升级中....");
        }
    }

}
